﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Helper;
using System.Data;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class HoTroTrucTuyenController : Controller
    {
        //
        // GET: /administrator/HoTroTrucTuyen/
        SaleSoftEntities db = new SaleSoftEntities();
        public ActionResult Index()
        {
            List<ds_hotrotructuyen> _ds_hotrotructuyen = db.ds_hotrotructuyen.ToList();
            return View(_ds_hotrotructuyen);
        }

        public ActionResult Create()
        {
            ViewBag.id_loaihotrotructuyen = new SelectList(db.loai_hotrotructuyen, "id_loaihotrotructuyen", "tenloaihotro");
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(1, null);
            return View();
        }
        
        public ActionResult Edit(int id)
        {
            ds_hotrotructuyen _ds_hotrotructuyen = db.ds_hotrotructuyen.Single(d => d.id_hotrotructuyen == id);
            ViewBag.id_loaihotrotructuyen = new SelectList(db.loai_hotrotructuyen, "id_loaihotrotructuyen", "tenloaihotro");
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(1, null);
            return View(_ds_hotrotructuyen);
        }


        [HttpPost]
        public ActionResult Create(ds_hotrotructuyen _ds_hotrotructuyen)
        {
            if (ModelState.IsValid)
            {
                db.ds_hotrotructuyen.AddObject(_ds_hotrotructuyen);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.id_loaihotrotructuyen = new SelectList(db.loai_hotrotructuyen, "id_loaihotrotructuyen", "tenloaihotro", _ds_hotrotructuyen.id_loaihotrotructuyen);
            return View(_ds_hotrotructuyen);  
        }

        [HttpPost]
        public ActionResult Edit(ds_hotrotructuyen _ds_hotrotructuyen)
        {
            if (ModelState.IsValid)
            {
                db.ds_hotrotructuyen.Attach(_ds_hotrotructuyen);
                db.ObjectStateManager.ChangeObjectState(_ds_hotrotructuyen, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_loaihotrotructuyen = new SelectList(db.loai_hotrotructuyen, "id_loaihotrotructuyen", "tenloaihotro", _ds_hotrotructuyen.id_loaihotrotructuyen);
            return View(_ds_hotrotructuyen);  
        }

        public ActionResult Delete(int id)
        {
            ds_hotrotructuyen _ds_hotrotructuyen = db.ds_hotrotructuyen.Single(d => d.id_hotrotructuyen == id);
            db.ds_hotrotructuyen.DeleteObject(_ds_hotrotructuyen);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
